##
# Methods added to this helper will be available to all templates in 
# the application.
module ApplicationHelper
  ##
  # this method parses the image tags: [[picture.png|alternative text]]
  def parse_images(txt)
    txt = txt.gsub(/\[\[(.*?)\|(.*?)\]\]/, '<img src="/media/\1" alt="\2"/>')
    return txt
  end
  ##
  # this method returns the grade of the exam-result
  # @param exam is an instance of type exam-result
  # @return a integer is returned, which indicates the grade of the student
  def get_grade(exam)
    max = exam.max_score
    score = exam.score
    
    return 5 if max == 0
    percent = (score.to_f / max.to_f) * 100
    percent = percent.to_i
    tmp = exam.exam
    case 
      when percent > tmp.percent_1
        return 1
      when percent > tmp.percent_2
        return 2
      when percent > tmp.percent_3
        return 3
      when percent > tmp.percent_4
        return 4
      else
        return 5
    end
  end
end
